# coding: UTF-8
# Filename : chain_part_api.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-
from api.common.common import *
from api.common import mysql,log,utils,httputils,const
#import mysql,log,utils,httputils,const
import chain_part_data_model

common_sql = '''
select `name`, address, tel, longitude, latitude, description, store_in_app, id, images
from chain_part
'''

#替换字符
def replace_body(obj):
    if obj == None : return ''
    body_template = chain_part_data_model.imedia_body
    body_template = body_template.replace('{name}', str(obj[0]))
    body_template = body_template.replace('{address}', str(obj[1]))
    body_template = body_template.replace('{tel}', str(obj[2]))
    body_template = body_template.replace('{longitude}', str(obj[3]))
    body_template = body_template.replace('{latitude}', str(obj[4]))
    body_template = body_template.replace('{description}', str(obj[5]))
    body_template = body_template.replace('{storeinapp}', str(obj[6]))
    body_template = body_template.replace('{id}', str(obj[7]))
    body_template = body_template.replace('{images}', const.domain + str(obj[8]))
    return body_template    
  
def get_detail_str(part_id):
    #数据库查询
    sql = common_sql + '''
    where id = %s
    and active = 1
    '''
    obj = mysql.get(sql, [int(part_id)])
    return replace_body(obj)

#获取中间数据体
def get_body_str(cus_id, size, pagenum):
    #sql
    sql = common_sql + '''
    where cus_id = %s
    and active = 1
    limit %s, %s
    '''
    #获取起始行
    start_row = utils.get_star_row(size, pagenum)
    param = [int(cus_id), start_row, int(size)]
    rsts = mysql.doSearch(sql, param)
    body_str = ''
    for obj in rsts:
        body_str += replace_body(obj)
    return body_str


#获取详细列表http://api.imedia.com/app/part/list
def chain_part_list(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #获取请求参数
    cus_id = httputils.post(request, 'cus_id')
    size = httputils.post(request, 'size')
    pagenum = httputils.post(request, 'pagenum')
    if not utils.is_num(size): size = 5
    if not utils.is_num(pagenum): pagenum = 1
    if not utils.is_num(cus_id): cus_id = 1
    #通信验证
    if validate(request):
        return validate(request)
    #中间模板段
    output = ''
    output += chain_part_data_model.imedia_start 
    output += get_body_str(cus_id, size, pagenum)
    output += chain_part_data_model.imedia_end
    #初始化xml返回
    return response(output)

#获取单个信息http://api.imedia.com/app/part
def chain_part_detail(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    
    part_id = httputils.post(request, 'part_id')
    if not utils.is_num(part_id): part_id = 0 #做验证防止异常发生
    output = ''
    output += chain_part_data_model.imedia_start
    output += get_detail_str(part_id)
    output += chain_part_data_model.imedia_end
    return response(output)


